def bubble_sort(arr):
    """
    Sort an array using the bubble sort algorithm.

    Time complexity: O(n^2)
    Space complexity: O(1)

    :param arr: The array to sort
    :return: The sorted array
    """
    n = len(arr)
    # 遍历数组 n-1 次
    # Iterate through the array n-1 times
    is_sorted = False
    while not is_sorted:
        is_sorted = True
        for j in range(n-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                is_sorted = False
    return arr



